package com.example.a13001.shoppingmalltemplate.activitys;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.utils.KeyboardUtils;
import com.example.a13001.shoppingmalltemplate.R;
import com.example.a13001.shoppingmalltemplate.adapters.QuestionLvAdapter;
import com.example.a13001.shoppingmalltemplate.base.AppConstants;
import com.example.a13001.shoppingmalltemplate.base.BaseActivity;
import com.example.a13001.shoppingmalltemplate.modle.AnswerList;
import com.example.a13001.shoppingmalltemplate.modle.CommonResult;
import com.example.a13001.shoppingmalltemplate.mvpview.QuestionAndAnswerView;
import com.example.a13001.shoppingmalltemplate.presenter.QuestionAndAnswerPredenter;
import com.example.a13001.shoppingmalltemplate.utils.MyUtils;
import com.example.a13001.shoppingmalltemplate.utils.SPUtils;
import com.example.a13001.shoppingmalltemplate.utils.Utils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class QuestionAndAnswerActivity extends BaseActivity {

    @BindView(R.id.iv_title_back)
    ImageView ivTitleBack;
    @BindView(R.id.tv_title_center)
    TextView tvTitleCenter;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.lv_qanda)
    ListView lvQanda;
    @BindView(R.id.et_qanda)
    EditText etQanda;
    @BindView(R.id.tv_qanda_release)
    TextView tvQandaRelease;
    @BindView(R.id.srfl_qanda)
    SmartRefreshLayout srflQanda;
    private QuestionLvAdapter mAdapter;
    private List<AnswerList.ListBean> mList;
    private static final String TAG = "QuestionAndAnswerActivi";
    QuestionAndAnswerPredenter questionAndAnswerPredenter = new QuestionAndAnswerPredenter(QuestionAndAnswerActivity.this);
    private String code;
    private String timestamp;
    private int mGoodsId;
    private int pageindex = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_question_and_answer);
        ButterKnife.bind(this);
        initData();
        setRefresh();
    }

    /**
     * 初始化数据源
     */
    private void initData() {
        tvTitleCenter.setText("问答");
        String safetyCode = MyUtils.getMetaValue(QuestionAndAnswerActivity.this, "safetyCode");
        code = Utils.md5(safetyCode + Utils.getTimeStamp());
        timestamp = Utils.getTimeStamp();
        mGoodsId = getIntent().getIntExtra("goodid", 0);
        questionAndAnswerPredenter.onCreate();
        questionAndAnswerPredenter.attachView(questionAndAnswerView);
        mList = new ArrayList<>();
        mAdapter = new QuestionLvAdapter(QuestionAndAnswerActivity.this, mList);
        Log.e(TAG, "initData: " + AppConstants.COMPANY_ID + mGoodsId + "" + "" + AppConstants.PAGE_SIZE + pageindex + AppConstants.FROM_MOBILE);
        questionAndAnswerPredenter.getCommentList(AppConstants.COMPANY_ID, String.valueOf(mGoodsId), "", "", AppConstants.PAGE_SIZE, pageindex, AppConstants.FROM_MOBILE);
        lvQanda.setAdapter(mAdapter);
    }

    QuestionAndAnswerView questionAndAnswerView = new QuestionAndAnswerView() {
        @Override
        public void onSuccessCommitComment(CommonResult commonResult) {
            Log.e(TAG, "onSuccessCommitComment: " + commonResult.toString());
            int status = commonResult.getStatus();
            if (status > 0) {
                Toast.makeText(QuestionAndAnswerActivity.this, "提交成功", Toast.LENGTH_SHORT).show();
                KeyboardUtils.hideSoftInput(QuestionAndAnswerActivity.this);
                etQanda.setText("");
            } else {
                Toast.makeText(QuestionAndAnswerActivity.this, commonResult.getReturnMsg(), Toast.LENGTH_SHORT).show();
                KeyboardUtils.hideSoftInput(QuestionAndAnswerActivity.this);
            }
        }

        @Override
        public void onSuccessGetCommentList(AnswerList answerList) {
            Log.e(TAG, "onSuccessGetCommentList: " + answerList.toString());
            int status = answerList.getStatus();
            if (status > 0) {
                mList.addAll(answerList.getList());
                mAdapter.notifyDataSetChanged();
            } else {

            }
        }

        @Override
        public void onError(String result) {
            Log.e(TAG, "onError: " + result.toString());
        }
    };

    @OnClick({R.id.iv_title_back, R.id.tv_qanda_release})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            //返回
            case R.id.iv_title_back:
                onBackPressed();
                break;
            //发布
            case R.id.tv_qanda_release:
                String question = etQanda.getText().toString().trim();
                if (TextUtils.isEmpty(question)) {
                    Toast.makeText(this, "请输入问题", Toast.LENGTH_SHORT).show();
                    return;
                }
                String username = (String) SPUtils.get(QuestionAndAnswerActivity.this, AppConstants.USER_NAME, "游客");
                Log.e(TAG, "onViewClicked: " + AppConstants.COMPANY_ID + "==code==" + code + "==timestamp==" + timestamp + "==mGoodsId==" + String.valueOf(mGoodsId) + "==timestamp==" + "游客" + "==question==" + question + "");
                questionAndAnswerPredenter.doCommentAdd(AppConstants.COMPANY_ID, code, timestamp, String.valueOf(mGoodsId), "", question, "");
                break;
        }
    }

    private void setRefresh() {
        //刷新
        srflQanda.setRefreshHeader(new ClassicsHeader(QuestionAndAnswerActivity.this));
        srflQanda.setRefreshFooter(new ClassicsFooter(QuestionAndAnswerActivity.this));
//        mRefresh.setEnablePureScrollMode(true);
        srflQanda.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                pageindex = 1;
                if (mList != null) {
                    mList.clear();
                }
                questionAndAnswerPredenter.getCommentList(AppConstants.COMPANY_ID, String.valueOf(mGoodsId), "", "", AppConstants.PAGE_SIZE, pageindex, AppConstants.FROM_MOBILE);
                refreshlayout.finishRefresh(2000/*,true*/);//传入false表示刷新失败
            }
        });
        srflQanda.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                pageindex++;
                questionAndAnswerPredenter.getCommentList(AppConstants.COMPANY_ID, String.valueOf(mGoodsId), "", "", AppConstants.PAGE_SIZE, pageindex, AppConstants.FROM_MOBILE);
                refreshlayout.finishLoadMore(2000/*,true*/);//传入false表示加载失败
            }
        });
    }
}
